package com.irdstudio.efp.esb.api.service.yed;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.irdstudio.basic.framework.core.constant.EsbRetCodeStandard;
import com.irdstudio.basic.framework.core.exception.BizException;
import com.irdstudio.efp.cus.service.vo.CompanyWhiteListVO;
import com.irdstudio.efp.cus.service.vo.PersonalWhiteListVO;
import com.irdstudio.efp.esb.api.common.EsbRespServiceBeanCreator;
import com.irdstudio.efp.esb.api.rest.yed.req.ReqCompanyWhiteBean;
import com.irdstudio.efp.esb.api.rest.yed.req.ReqPersonalWhiteBean;
import com.irdstudio.efp.esb.api.rest.yed.resp.RespCompanyWhiteBean;
import com.irdstudio.efp.esb.api.rest.yed.resp.RespPersonalWhiteBean;
import com.irdstudio.efp.esb.api.rest.yed.resp.WhtlistInfArry;
import com.irdstudio.efp.esb.common.constant.EsbSysHeadEnums;
import com.irdstudio.efp.esb.common.server.req.EsbReqServiceBean;
import com.irdstudio.efp.esb.common.server.resp.EsbRespServiceBean;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

@Service
public class YedPersonalWhiteListService {

    private static Logger logger = LoggerFactory.getLogger(YedPersonalWhiteListService.class);


    public WhtlistInfArry getLoanInfArry(PersonalWhiteListVO listVO) throws Exception {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        WhtlistInfArry whtlistInfArry = new WhtlistInfArry();

        //公司名称
        whtlistInfArry.setCoNm(listVO.getCompanyName());
        //公司类型
        whtlistInfArry.setCoTp("");
        //借款人姓名
        whtlistInfArry.setBrwNm(listVO.getCusName());
        //证件号码
        whtlistInfArry.setIdentNo(listVO.getCertCode());
        //导入人员工号
        whtlistInfArry.setImprPrsnStfNo(listVO.getImportEmpNo());
        //上报机构
        whtlistInfArry.setRprtBrch(listVO.getAppearOrg());
        //最后更新日期
        whtlistInfArry.setLastUdtDt(listVO.getUpdateDate());
        //最后更新人员工号
        whtlistInfArry.setLastUdtPrsnStfNo(listVO.getUpdateNo());
        //证件类型
        whtlistInfArry.setIdnetTp(listVO.getCertType());
        //有效期开始时间
        whtlistInfArry.setVldtyBegTm("");
        //有效期结束时间
        whtlistInfArry.setVldtyEndTm("");
        //有效期天数
        whtlistInfArry.setVldtyDays(listVO.getEffectiveDate());
        //导入日期
        whtlistInfArry.setImprDt(listVO.getImportDate());
        //导入人员名称
        whtlistInfArry.setImprPrsnNm(listVO.getImportEmpName());
        //最后更新人员名称
        whtlistInfArry.setLastUdtPrsnNm(listVO.getUpdateName());
        //职位
        whtlistInfArry.setPsitn(listVO.getPositionPost());
        //企业所在城市
        whtlistInfArry.setEntpWrbtAddr(listVO.getCompanyCity());
        //企业所在地址
        whtlistInfArry.setEntpWrbtCity(listVO.getCompanyCity());
        //收入
        whtlistInfArry.setIncm(listVO.getSalary());

        return whtlistInfArry;

    }

    public void setRespPersonalWhiteBean(String pageNo, String pageSize, RespPersonalWhiteBean respPersonalWhiteBean, Integer totalSize, ArrayList<WhtlistInfArry> loanInfArries) {

        respPersonalWhiteBean.setRetMsg("查询成功");
        respPersonalWhiteBean.setRetStCd(EsbSysHeadEnums.RetStEnum.SUCCESS.getCode());
        respPersonalWhiteBean.setWhtlistInfArryList(loanInfArries);
    }

    /**
     * 发生异常时候封装EsbRespServiceBean
     *
     * @param esbReqServiceBean
     * @param exception
     * @param pageNo
     * @param pageSize
     * @return
     */
    public EsbRespServiceBean getEsbRespServiceBean(EsbReqServiceBean esbReqServiceBean, Exception exception, String pageNo, String pageSize, RespPersonalWhiteBean respPersonalWhiteBean, EsbRetCodeStandard esbRetCodeStandard) {
        logger.error("优e贷个人白名单查询发生异常：" + exception.getMessage());
        //需要反馈异常信息给esb
        logger.info("优e贷个人白名单查询发生异常处理开始");
        //当返回异常时，设置查询到的记录数为0
        respPersonalWhiteBean.setRetStCd(esbRetCodeStandard.getValue());
        respPersonalWhiteBean.setRetMsg(exception.getMessage());
        respPersonalWhiteBean.setWhtlistInfArryList(new ArrayList<WhtlistInfArry>());
        logger.info("优e贷个人白名单查询发生异常处理结束");
        exception.printStackTrace();
        return EsbRespServiceBeanCreator.buildesbRespServiceBean(esbReqServiceBean,
                JSONObject.toJSONString(respPersonalWhiteBean, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullListAsEmpty, SerializerFeature.WriteNullStringAsEmpty, SerializerFeature.WriteNullNumberAsZero, SerializerFeature.WriteMapNullValue),
                EsbSysHeadEnums.RetStEnum.FAILURE.getCode(),
                esbRetCodeStandard.getValue(),
                exception.getMessage());
    }

    public PersonalWhiteListVO transferQueryParams(ReqPersonalWhiteBean reqPersonalWhiteBean) throws Exception{
        PersonalWhiteListVO listVO = new PersonalWhiteListVO();
        if(StringUtils.isBlank(reqPersonalWhiteBean.getPageIndex()) || StringUtils.isBlank(reqPersonalWhiteBean.getPageSize())){
            throw new BizException("查询页数不能为空");
        }
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //公司名称
        listVO.setCompanyName(reqPersonalWhiteBean.getCompanyName());
        //借款人姓名
        listVO.setCusName(reqPersonalWhiteBean.getCusName());
        //身份证类型
        listVO.setCertCode(reqPersonalWhiteBean.getCertCode());
        //导入时间
        listVO.setImportDate(reqPersonalWhiteBean.getImportTime());
        //导入人员工号
        listVO.setImportEmpNo(reqPersonalWhiteBean.getImportEmpNo());
        //上报机构
        listVO.setAppearOrg(reqPersonalWhiteBean.getAppearOrg());
        //最后更新日期
        listVO.setUpdateNo(reqPersonalWhiteBean.getUpdateNo());
        //最后更新人员工号
        listVO.setUpdateDate(reqPersonalWhiteBean.getUpdateDate());

        return listVO;
    }

    // 默认第一页
    private final String DEFAULT_PAGE_NO = "1";
    // 默认每页显示10条记录
    private final String DEFAULT_PAGE_SIZE = "10";
    /**
     * 处理查询到的数据
     * @param pageNo
     * @param pageSize
     * @return
     * @throws Exception
     */
    public List<PersonalWhiteListVO> handleResultList(List<PersonalWhiteListVO> whiteListVOList, String pageNo, String pageSize) throws Exception {
        int intPageNo = 0;
        int intPageSize = 0;
        int totalSize = whiteListVOList.size();

        if (StringUtils.isEmpty(pageNo)) {
            // 没有传入页码或者页码非法时候默认第一页
            pageNo = DEFAULT_PAGE_NO;
        }
        if (StringUtils.isEmpty(pageSize)) {
            // 没有传入显示几行数据时，默认显示10条数据
            pageSize = DEFAULT_PAGE_SIZE;
        }
        intPageNo = Integer.valueOf(pageNo);
        intPageSize = Integer.valueOf(pageSize);

        List<PersonalWhiteListVO> newPersonalWhiteListVO = new ArrayList<PersonalWhiteListVO>();
        for (int i = intPageSize * (intPageNo - 1); i < totalSize && i < intPageSize * intPageNo; i++) {
            newPersonalWhiteListVO.add(whiteListVOList.get(i));
        }
        return newPersonalWhiteListVO;
    }

}
